package com.tan.kotlintest.adapter

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.tan.kotlintest.R

/*
 * Copyright (C) 2018 重庆一聊高科技有限公司
 * 版权所有
 *
 * 功能描述：测试Kotlin工程  主页面ListView 的adapter
 * 作者：txj
 * 创建时间：2018/9/5
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
class MainListAdapter(private var mContext: Context) : BaseAdapter() {

    private lateinit var mBeans: Array<String>

    fun loadData(beans: Array<String>) {
        mBeans = beans
    }

    override fun getItem(position: Int): Any {
        return mBeans[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return mBeans.size
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val bean: String = getItem(position) as String
        val holder: Holder
        val view: View
        if (convertView == null) {
            holder = Holder()
            view = LayoutInflater.from(mContext).inflate(R.layout.item_main_list_layout, parent, false)
            holder.mContentTv = view.findViewById(R.id.content_tv)
            view.tag = holder
        } else {
            view = convertView
            holder = convertView.tag as Holder
        }
        if (!TextUtils.isEmpty(bean)) {
            holder.mContentTv.text = bean
        }
        return view
    }

    class Holder {
        lateinit var mContentTv: TextView
    }

}